﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LIM.model;

namespace LIM.controller
{
    public class DanhMucBaoGiaController : BaseController
    {
        public DanhMucBaoGiaController()
        {
        }
        public object Load()
        {
            List<tbl_CRM_BaoGia> cas = LIMS_Entities.tbl_CRM_BaoGia.ToList();
            return LIMS_Entities.tbl_CRM_BaoGia.ToList().Select(p => new 
                                                            {
                                                                RowID = p.RowID,
                                                                MaBaoGia = p.MaBaoGia,
                                                                KhachHang = GetKH(p.RowIDKhachHang),
                                                                DiaChi = p.DiaChi,
                                                                NguoiLienHe = GetLienHe(p.RowIDLienHe),
                                                                GhiChu = p.GhiChu,
                                                                TinhTrang = p.TinhTrang,
                                                                NhanVienTao = p.NguoiTao,
                                                                NgayTao = p.NgayTao,
                                                                //FileDinhKem = p.FileDinhKem
                                                            }).ToList();
        }
        public tbl_CRM_BaoGia LoadBaoGiaByID(int RowID)
        {
            return LIMS_Entities.tbl_CRM_BaoGia.Where(p => p.RowID == RowID).FirstOrDefault();
        }
        public List<v_BaoGiaChiTieu> LoadBaoGiaChiTieuByRowIDBaoGia(int RowID)
        {
            return LIMS_Entities.v_BaoGiaChiTieu.Where(p => p.RowIDBaoGia == RowID).ToList();
        }
        public string GetKH(int? RowIDKH)
        {
            string result = "";
            if (RowIDKH != null)
            {
                tbl_CRM_KhachHang a = LIMS_Entities.tbl_CRM_KhachHang.Where(p => p.RowID == RowIDKH).FirstOrDefault();
                if (a != null)
                {
                    result = a.TenGoi;
                }
            }
            return result;
        }
        public  string GetLienHe(int? RowIDLienHe)
        {
            string result = "";
            if (RowIDLienHe != null)
            {
                tbl_CRM_LienHe a = LIMS_Entities.tbl_CRM_LienHe.Where(p => p.RowID == RowIDLienHe).FirstOrDefault();
                if (a != null)
                {
                    result = a.HoTen;
                }
            }
            return result;
        }
        public tbl_DanhMuc_HeThong LoadDataByRowID(int RowID)
        {
            return LIMS_Entities.tbl_DanhMuc_HeThong.Where(p => p.RowID == RowID).FirstOrDefault();
        }
        public List<tbl_DanhMuc_HeThong> LoadDataList(string MaDanhMuc)
        {
            return LIMS_Entities.tbl_DanhMuc_HeThong.Where(p => p.MaDanhMuc == MaDanhMuc).OrderBy(p => p.ThuTu).ToList();
        }
        public List<tbl_DanhMuc_HeThong> LoadDataChild(int ParentID)
        {
            return LIMS_Entities.tbl_DanhMuc_HeThong.Where(p => p.RowIDParent == ParentID).OrderBy(p => p.ThuTu).ToList();
        }
        public List<object> LoadBaoGia(bool? TinhTrang, string LoaiTinhTrang, bool IsOlder, int nam)
        {
            List<tbl_CRM_BaoGia> lstBaoGia = new List<tbl_CRM_BaoGia>();
            if (TinhTrang == null)
            {
                lstBaoGia = LIMS_Entities.tbl_CRM_BaoGia.ToList();
            }
            else if (TinhTrang.Value)
            {
                lstBaoGia = LIMS_Entities.tbl_CRM_BaoGia.Where(p => p.TinhTrang == LoaiTinhTrang).ToList();
            }
            else
            {
                if (IsOlder)
                {
                    lstBaoGia = LIMS_Entities.tbl_CRM_BaoGia.ToList().Where(p => p.TinhTrang == LoaiTinhTrang && p.NgayTao.HasValue && p.NgayTao.Value.Year < nam).ToList();
                }
                else
                {
                    lstBaoGia = LIMS_Entities.tbl_CRM_BaoGia.ToList().Where(p => p.TinhTrang == LoaiTinhTrang && p.NgayTao.HasValue && p.NgayTao.Value.Year == nam).ToList();
                }
            }
            return lstBaoGia.Select(p => new
            {
                RowID= p.RowID,
                MaBaoGia = p.MaBaoGia,
                KhachHang = GetKH(p.RowIDKhachHang),
                DiaChi = p.DiaChi,
                NguoiLienHe = GetLienHe(p.RowIDLienHe),
                GhiChu = p.GhiChu,
                TinhTrang = p.TinhTrang != null ? LIMS_Entities.tbl_DanhMuc_HeThong.Where(o => o.MaDanhMuc == p.TinhTrang).FirstOrDefault().TenGoi : "",
                NgayKy = p.NgayTao,
                NgayKetThuc = p.ThoiGianHieuLuc_Den,
                NhanVienTao = p.NguoiTao,
                NgayTao = p.NgayTao,
            }).ToList<object>();
        }
        public List<tbl_DanhMuc_LoaiDichVuKiemNghiem> LoadDM_LoaiDichVuKiemNghiem()
        {
            return LIMS_Entities.tbl_DanhMuc_LoaiDichVuKiemNghiem.ToList();
        }
        public List<tbl_DanhMuc_LoaiMau> LoadDM_LoaiMau(int RowIDLoaiDichVuKiemNghiem)
        {
            return LIMS_Entities.tbl_DanhMuc_LoaiMau.Where(p=>p.RowIDLoaiDichVuKiemNghiem == RowIDLoaiDichVuKiemNghiem).ToList();
        }
        public object LoadDM_ChiTieu(int RowIDLoaiDichVuKiemNghiem, int RowIDLoaiMau)
        {
            List<tbl_DanhMuc_ChiTieu> lstDanhMuc_ChiTieu = new List<tbl_DanhMuc_ChiTieu>();
            if (RowIDLoaiMau != 0)
            { 
                tbl_DanhMuc_LoaiMau loaimau = LIMS_Entities.tbl_DanhMuc_LoaiMau.Where(p => p.RowID == RowIDLoaiMau).FirstOrDefault();
                if (loaimau != null)
                {
                    lstDanhMuc_ChiTieu = loaimau.tbl_DanhMuc_ChiTieu.ToList();
                }
            }
            else if (RowIDLoaiDichVuKiemNghiem != 0)
            {
                lstDanhMuc_ChiTieu = LIMS_Entities.tbl_DanhMuc_ChiTieu.Where(p => p.tbl_DanhMuc_LoaiMau.tbl_DanhMuc_LoaiDichVuKiemNghiem.RowID == RowIDLoaiDichVuKiemNghiem).ToList();
            }
            return lstDanhMuc_ChiTieu.Select(p => new 
                                                {
                                                    MaChiTieu = p.MaChiTieu,
                                                    TenChiTieu=p.TenGoi,
                                                    Gia = GetGiaChiTieu(p.RowID),
                                                    PhuongPhap=p.PhuongPhap,
                                                    Mau  = p.tbl_DanhMuc_LoaiMau.TenGoi
                                                }).ToList();
        }

        public object LoadDM_ChiTieu_ByID(List<ModelCustomize.ClassOneDegitInt> lst)
        {
            List<tbl_DanhMuc_ChiTieu> lstDanhMuc_ChiTieu = new List<tbl_DanhMuc_ChiTieu>();
            foreach (ModelCustomize.ClassOneDegitInt item in lst)
            {
                tbl_DanhMuc_LoaiMau loaimau = LIMS_Entities.tbl_DanhMuc_LoaiMau.Where(p => p.RowID == item.Value).FirstOrDefault();
                if (loaimau != null)
                {
                    lstDanhMuc_ChiTieu.AddRange(loaimau.tbl_DanhMuc_ChiTieu.ToList());
                }
            }
            return lstDanhMuc_ChiTieu.Select(p => new
            {
                MaChiTieu = p.MaChiTieu,
                TenChiTieu = p.TenGoi,
                Gia = GetGiaChiTieu(p.RowID),
                PhuongPhap = p.PhuongPhap,
                Mau = p.tbl_DanhMuc_LoaiMau.TenGoi
            }).ToList();
        }

        decimal? GetGiaChiTieu(int? RowIDChiTieu)
        {
            decimal? gia = null;
            if (RowIDChiTieu != null)
            {
                tbl_DanhMuc_ChiTieu_BangGia banggia = LIMS_Entities.tbl_DanhMuc_ChiTieu_BangGia.Where(p => p.RowIDChiTieu == RowIDChiTieu).OrderByDescending(o => o.NgayHieuLuc).FirstOrDefault();
                if (banggia != null)
                {
                    gia = banggia.Gia;
                }
            }
            return gia;
        }
    }
}
